package com.changjiang.luban.utils.result;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:API 统一返回状态码
 * @author nichangjiang@vip.qq.com
 * 创建时间: 2017/12/11. 15:39
 * 修改人:
 * 修改时间:
 * 修改备注:
 * 版本:v1.0
 */
public enum ApiResultCode {

    /** 成功状态码 */
    SUCCESS(1,"成功"),
    OPERATION_FAILED(2,"操作失败"),

    /** 系统错误状态码 -1 ... -9999 */
    SYSTEM_INNER_ERROR(-1, "系统繁忙，请稍后重试"),
    RESOURCE_NOT_FOUND(-2, "文件资源不存在"),
    SQL_INVALID(-3, "sql查询异常"),
    OSS_FILE_UPLOAD(-4, "OSS文件上传异常"),
    NOT_FOUND(-5,"接口不存在"),
    IMG_UPLOAD(-6,"图片上传错误"),

    /** 参数错误状态码 10001-19999 */
    PARAM_IS_INVALID(10001, "参数无效"),
    PARAM_IS_BLANK(10002, "参数为空"),
    PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
    PARAM_NOT_COMPLETE(10004, "参数缺失"),


    /** 用户错误状态码 20000-29999 */
    USER_REGISTRATION(20000, "注册失败"),
    USER_NOT_TOKEN(20001, "用户token不合法或者已失效"),
    USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
    USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
    USER_NOT_EXIST(20004, "用户不存在"),
    USER_HAS_EXISTED(20005, "用户已存在"),
    USER_PHONE_EXISTED(20006, "手机号码已存在"),
    USER_BANK_CARD_EXISTED(20007, "银行卡已存在"),
    USER_PASSWORD_ERROR(20009,"原密码错误"),

    /** 用户参加活动状态码错误 21000-21999*/
    USER_TASK_RECEIVE_ERROR(21000,"领取任务失败"),
    USER_NOT_RECEIVE_COUPON(21001,"优惠券不能领取"),
    USER_NOT_RECEIVE_COUPON_ERROR(21002,"领取优惠券失败"),

    /** 数据错误状态码 50001-59999 */
    DATA_NONE(50001,"暂无数据"),
    DATA_IS_WRONG(50002, "数据有误"),
    DATA_ALREADY_EXISTED(50003, "数据已存在"),

    /** 信息验证失败 60001-69999 */
    MERCHANDISE_UNDER_THE_SHELF(600001,"商品已下架"),

    /** 权限错误状态码 70001-79999*/
    PERMISSION_NO_ACCESS(70001, "无访问权限");

    private Integer code;

    private String message;

    ApiResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer code() {
        return this.code;
    }

    public String message() {
        return this.message;
    }

    public static String getMessage(String name) {
        for (ApiResultCode item : ApiResultCode.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ApiResultCode item : ApiResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }

    public static void main(String[] args) {
        ApiResultCode[] ApiResultCodes = ApiResultCode.values();
        List<Integer> codeList = new ArrayList<Integer>();
        for (ApiResultCode ApiResultCode : ApiResultCodes) {
            if (codeList.contains(ApiResultCode.code)) {
                System.out.println(ApiResultCode.code);
            } else {
                codeList.add(ApiResultCode.code());
            }
        }
    }

}
